package com.wan37.logic.dao.impl;

import com.wan37.logic.dao.RoleDao;
import com.wan37.logic.domain.entity.RoleDO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-06-01 17:58
 **/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false, rollbackFor = Exception.class)
@Repository
public class RoleDaoImpl implements RoleDao {

    @Autowired
    HibernateTemplate hibernateTemplate;


    @Override
    public boolean add(RoleDO roleDO) {
        try {
            hibernateTemplate.save(roleDO);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer roleId) {
        try {
            hibernateTemplate.delete(roleId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(RoleDO roleDO) {
        try {
            Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            Query query = session.createQuery("update RoleDO set nickName=?, status=?,userId=?,level=?,MP=?,HP=? where roleId=?");
            query.setParameter(0, roleDO.getNickname());
            query.setParameter(1, roleDO.getStatus());
            query.setParameter(2, roleDO.getUserId());
            query.setParameter(3, roleDO.getLevel());
            query.setParameter(4, roleDO.getMP());
            query.setParameter(5, roleDO.getHP());
            query.setParameter(6, roleDO.getRoleId());
            query.executeUpdate();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public RoleDO getRoleByRoleId(String roleId) {
        List<RoleDO> list = (List<RoleDO>) hibernateTemplate.find("from RoleDO where roleId = ?", roleId);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public RoleDO getRoleByNickname(String nickname) {
        List<RoleDO> list = (List<RoleDO>) hibernateTemplate.find("from RoleDO where nickname = ?", nickname);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }


    @Override
    public List<RoleDO> getRoleByUserId(String userId) {
        return (List<RoleDO>) hibernateTemplate.find("from RoleDO where userId = ?", userId);
    }

    @Override
    public List<RoleDO> getAll() {


        return (List<RoleDO>) hibernateTemplate.find("from RoleDO");
    }
}
